package com.hanxiaozhang.base.service.impl;

import com.hanxiaozhang.base.KeyValuePair;
import com.hanxiaozhang.base.dao.DictDao;
import com.hanxiaozhang.base.dto.FileDTO;
import com.hanxiaozhang.base.entity.DictEntity;
import com.hanxiaozhang.constant.DelFlagEnum;
import com.hanxiaozhang.security.CurrentUserUtil;
import com.hanxiaozhang.util.FileUtil;
import com.hanxiaozhang.util.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.hanxiaozhang.base.dao.FileDao;
import com.hanxiaozhang.base.entity.FileEntity;
import com.hanxiaozhang.base.service.FileService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


/**
 * 文件表
 *
 * @author hanxiaozhang
 * @email hanxiaozhang2018@sina.com
 * @date 2021-05-02
 */
@Service
public class FileServiceImpl implements FileService {

    @Resource
    private FileDao fileDao;
    @Resource
    private DictDao dictDao;

    @Value("${hanxiaozhang.baseFilePath}")
    private String baseFilePath;

    @Value("${hanxiaozhang.fileUrlPrefix}")
    private String fileUrlPrefix;

    @Override
    public FileDTO get(Long fileId) {
        return fileDao.get(fileId);
    }

    @Override
    public List<FileDTO> list(Map<String, Object> map) {
        return fileDao.list(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(FileEntity file) {
        file.setUpdateBy(CurrentUserUtil.getUserName())
                .setUpdateDate(LocalDateTime.now());
        return fileDao.update(file);
    }

    @Override
    public int remove(Long fileId) {
        return fileDao.remove(fileId);
    }

    @Override
    public int batchRemove(Long[] fileIds) {
        return fileDao.batchRemove(fileIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FileDTO uploadFile(MultipartFile file, String type, String fileTag) throws Exception {

         FileDTO fileDTO = new FileDTO();
        String fileName = file.getOriginalFilename();
        String preName = fileName.substring(0, fileName.lastIndexOf("."));
        String extName = fileName.substring(fileName.lastIndexOf(".") + 1);

        String fileLocalPath = "";
        if (StringUtil.isNotBlank(type)) {
            fileLocalPath = type;
            if (StringUtil.isNotBlank(fileTag)) {
                fileLocalPath = fileLocalPath + FileUtil.UNIX_SEPARATOR + fileTag;
            }
        }
        // 本地文件名称
        String localFileName = FileUtil.fileNamePlusUUID(preName, extName);
        String filePath = baseFilePath + fileLocalPath + FileUtil.UNIX_SEPARATOR;
        // 上传文件
        FileUtil.uploadFile(file.getBytes(), filePath, localFileName);

        filePath = filePath + localFileName;

        String fileUrl = fileUrlPrefix + fileLocalPath + FileUtil.UNIX_SEPARATOR + localFileName;

        //封装数据
        fileDTO.setFileName(preName)
                .setFileExt(extName)
                .setFileFull(fileName)
                .setUrl(fileUrl)
                .setType(type)
                .setFileTag(fileTag)
                .setFilePath(filePath)
                .setCreateBy(CurrentUserUtil.getUserName())
                .setCreateDate(LocalDateTime.now())
                .setDelFlag(DelFlagEnum.NORMAL.getCode());
        fileDao.save(fileDTO);
        //处理数据
        DictEntity dict = dictDao.getByTypeAndValue(type, fileTag);
        if (dict != null) {
            fileDTO.setTypeName(dict.getDescription())
                    .setFileTagName(dict.getName());
        }
        fileDTO.setUuid(UUID.randomUUID().toString());
        return fileDTO;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdateBelongIdByFileId(List<KeyValuePair<Long, Long>> fileIdAndBelongIdList) {
        if (!fileIdAndBelongIdList.isEmpty()) {
            for (KeyValuePair<Long, Long> FileIdAndBelongId : fileIdAndBelongIdList) {
                FileEntity file = new FileEntity();
                file.setFileId(FileIdAndBelongId.getK())
                        .setBelongId(FileIdAndBelongId.getV())
                        .setUpdateBy(CurrentUserUtil.getUserName())
                        .setUpdateDate(LocalDateTime.now());
                fileDao.update(file);
            }
        }
    }


}
